mod sample;

use ggez::{Context, GameResult, GameError, graphics};
use ggez::mint::Point2;
use rand_distr::{Distribution, Normal};

pub struct State{
    samples: Vec<sample::Sample>,
}

impl State{
    pub fn new() -> State {
        //Due to the fact that ggez redraws the canvas every time, I store the previous walker states in a vector. It is not necessary to do this, I just wanted to see a similar visualization.
        State{samples : vec![]}
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        let mut rnd = rand::thread_rng();
        let normal = Normal::new(320.0, 60.0).unwrap();
        let u = normal.sample(&mut rnd);
        let v = normal.sample(&mut rnd);
        let sample = sample::Sample::new(Point2 { x: u, y: v });
        self.samples.push(sample);
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);

        for sample in &self.samples {
            let radius = 50.0/((sample.get_location().x-320.0)*(sample.get_location().x-320.0) + (sample.get_location().y-320.0)*(sample.get_location().y-320.0)).sqrt();
            let circle = graphics::Mesh::new_circle(
                ctx,
                graphics::DrawMode::fill(),
                sample.get_location(),
                radius,
                0.1,
                graphics::Color { r: sample.get_location().x/500.0, g: sample.get_location().y/500.0, b: radius/2.0, a: 1.0 },
            )?;
            canvas.draw(&circle, graphics::DrawParam::default());
        }

        canvas.finish(ctx)?;
        Ok(())
    }
}